package com.teamawesome.hackathon;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ExecutionException;

import com.teamawesome.hackathon.backend.ApiCalls;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
import org.json.*;

public class HackathonActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colours, android.R.layout.simple_spinner_item); 
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		final Spinner catSpinner = (Spinner)findViewById(R.id.SpinnerCATEGORY);
		catSpinner.setAdapter(adapter);

		ArrayAdapter locadapter = ArrayAdapter.createFromResource( this, R.array.location, android.R.layout.simple_spinner_item); 
		locadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		final Spinner locSpinner = (Spinner)findViewById(R.id.SpinnerLOCATION);
		locSpinner.setAdapter(locadapter);


		Button btnGo = (Button)findViewById(R.id.ButtonSearch);
		btnGo.setOnClickListener((new View.OnClickListener() {
			public void onClick(View v) {
				// Perform action on click


				String category = catSpinner.getSelectedItem().toString();
				String location = locSpinner.getSelectedItem().toString();

				category = getCatCode(category);


				TextView btnSearch = (TextView)findViewById(R.id.TextBoxSearchTerm);
				String searchTerm =  btnSearch.getText().toString();

				TextView minPrice = (TextView)findViewById(R.id.MinValueBox);
				String minPriceString = minPrice.getText().toString();

				TextView maxPrice = (TextView)findViewById(R.id.MaxValueBox);
				String maxPriceString = maxPrice.getText().toString();

				if (minPriceString.equalsIgnoreCase("")){
					minPriceString = "0";
				}

				if (maxPriceString.equalsIgnoreCase("")){
					maxPriceString = "999999999";
				}

				ApiCalls myCalls = new ApiCalls();
				final URL myUrl = myCalls.constructURL("E_BAY+OR+CRAIG", location, searchTerm, "10", minPriceString, maxPriceString);

				MyBackgroundTask t = new MyBackgroundTask();
				t.execute(myUrl);
				try {
					t.get();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				ResultParser parser = new ResultParser(response);

				Intent i = new Intent(getApplicationContext(), ResultsUi.class);
				i.putExtra("url",parser.urls());
				i.putExtra("image",parser.images());
				i.putExtra("title",parser.titles());
				i.putExtra("price", parser.price());
				startActivity(i);

			}
		}));

	}



	String response;

	class MyBackgroundTask extends AsyncTask<URL, Void, Void>{

		ProgressDialog dialog;

		protected void onPreExecute(){
			super.onPreExecute();
			dialog = ProgressDialog.show(HackathonActivity.this, "Getting Results", "Please Wait...");
		}

		protected Void doInBackground(URL... arg0) {
			ApiCalls api = new ApiCalls();
			try {
				response = api.getURLcontents(arg0[0]);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		protected void onPostExecute(Void unused){
			super.onPostExecute(unused);
			dialog.dismiss();
		}
	}

	private String getCatCode(String category){

		if (category.equals("Tickets"))
		{
			category="STIX";
		}
		else if 
		(category.equals("Tools"))
		{
			category="STOO";
		}
		else if 
		(category.equals("Tickets"))
		{
			category="STIX";
		}
		else if 
		(category.equals("Musical Instruments"))
		{
			category="SMUS";
		}
		else if 
		(category.equals("Industrial"))
		{
			category="SIND";
		}
		else if 
		(category.equals("Jewelry"))
		{
			category="SJWL";
		}
		else if 
		(category.equals("Home & Garden"))
		{
			category="SHNG";
		}
		else if 
		(category.equals("Furniture"))
		{
			category="SFUR";
		}
		else if 
		(category.equals("Electronics & Photo"))
		{
			category="SELE";
		}
		else if 
		(category.equals("Computers"))
		{
			category="SCOM";
		}
		else if 
		(category.equals("Bicycles"))
		{
			category="SBIK";
		}
		else if 
		(category.equals("Babies & Kids"))
		{
			category="SKID";
		}
		else if 
		(category.equals("Appliances"))
		{
			category="SAPL";
		}
		else if 
		(category.equals("Antiques"))
		{
			category="SANT";
		}
		else if             	
		(category.equals("Autos"))
		{
			category="VAUT";
		}
		else if 
		(category.equals("Pets"))
		{
			category="APET";
		}
		else if             	
		(category.equals("Vacation Properties"))
		{
			category="RVAC";
		}
		else if 
		(category.equals("House for Sale"))
		{
			category="RHFS";
		}
		else if             	
		(category.equals("House for Rent"))
		{
			category="RHFR";
		}
		return category;
	}
}